import Foundation

public enum Week: String, CaseIterable, Codable {
    case sun = "BearCalendar.week.sun"
    case mon = "BearCalendar.week.mon"
    case tue = "BearCalendar.week.tue"
    case wed = "BearCalendar.week.wed"
    case thu = "BearCalendar.week.thu"
    case fri = "BearCalendar.week.fri"
    case sat = "BearCalendar.week.sat"
    
    public var value: Int {
        switch self {
        case .sun: 0
        case .mon: 1
        case .tue: 2
        case .wed: 3
        case .thu: 4
        case .fri: 5
        case .sat: 6
        }
    }
    
    public func title(_ language: Language = Preference.shared.language) -> String {
        var bundle: Bundle = .module
        if let path = Bundle.module.path(forResource: language.locale.identifier, ofType: "lproj") {
            bundle = Bundle(path: path) ?? .module
        }
        return switch self {
        case .sun: NSLocalizedString(rawValue, bundle: bundle, comment: "")
        case .mon: NSLocalizedString(rawValue, bundle: bundle, comment: "")
        case .tue: NSLocalizedString(rawValue, bundle: bundle, comment: "")
        case .wed: NSLocalizedString(rawValue, bundle: bundle, comment: "")
        case .thu: NSLocalizedString(rawValue, bundle: bundle, comment: "")
        case .fri: NSLocalizedString(rawValue, bundle: bundle, comment: "")
        case .sat: NSLocalizedString(rawValue, bundle: bundle, comment: "")
        }
    }

    public func fullName(_ language: Language = Preference.shared.language) -> String {
        var bundle: Bundle = .module
        if let path = Bundle.module.path(forResource: language.locale.identifier, ofType: "lproj") {
            bundle = Bundle(path: path) ?? .module
        }
        let key = rawValue + ".full"
        return switch self {
        case .sun: NSLocalizedString(key, bundle: bundle, comment: "")
        case .mon: NSLocalizedString(key, bundle: bundle, comment: "")
        case .tue: NSLocalizedString(key, bundle: bundle, comment: "")
        case .wed: NSLocalizedString(key, bundle: bundle, comment: "")
        case .thu: NSLocalizedString(key, bundle: bundle, comment: "")
        case .fri: NSLocalizedString(key, bundle: bundle, comment: "")
        case .sat: NSLocalizedString(key, bundle: bundle, comment: "")
        }
    }

    // 从周几开始获取星期数组
    public static func weeks(beginAt week: Week = .sun) -> [Week] {
        switch week {
        case .sun: Week.allCases
        case .mon: [.mon, .tue, .wed, .thu, .fri, .sat, .sun]
        case .tue: [.tue, .wed, .thu, .fri, .sat, .sun, .mon]
        case .wed: [.wed, .thu, .fri, .sat, .sun, .mon, .tue]
        case .thu: [.thu, .fri, .sat, .sun, .mon, .tue, .wed]
        case .fri: [.fri, .sat, .sun, .mon, .tue, .wed, .thu]
        case .sat: [.sat, .sun, .mon, .tue, .wed, .thu, .fri]
        }
    }
    
}
